package com.ynet.eq.haiqing.module.mqtt.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ynet.eq.mqtt.consumer.MessageArrived;
import com.ynet.eq.haiqing.module.mqtt.MqttMessageContext;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Service;

/**
 * @author 胡超雄
 * @date 2021/6/30 17:00
 * @desc
 */
@Slf4j
@Service
public class MessageArrivedImpl implements MessageArrived {


    @Override
    public Boolean processMessage(String s, MqttMessage message) {

        log.info("接收消息开始处理了");
        String mess = new String(message.getPayload());
        JSONObject messJsonObj = JSON.parseObject(mess);
        String operator = messJsonObj.get("operator").toString();
        System.out.println(messJsonObj);
        //此处使用策略模式
        MqttMessageContext mmc = new MqttMessageContext(operator);
        if(mmc.hasNoStrategy()){
            log.error("处理消息失败,找不到策略实现类:"+operator);
            return  false ;
        }
        mmc.getResult(s, message);
        return null;
    }




}
